package com.website.web.back.controller.website;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.website.common.annotation.Log;
import com.website.common.enums.BusinessType;
import com.website.custom.domain.WNews;
import com.website.custom.service.IWNewsService;
import com.website.common.core.controller.BaseController;
import com.website.common.core.domain.AjaxResult;
import com.website.common.utils.poi.ExcelUtil;
import com.website.common.core.page.TableDataInfo;

/**
 * 新闻中心Controller
 * 
 * @author ruoyi
 * @date 2024-12-03
 */
@Controller
@RequestMapping("/back/custom/news")
public class WNewsController extends BaseController
{
    private String prefix = "back/website/news";

    @Autowired
    private IWNewsService wNewsService;

    @RequiresPermissions("system:news:view")
    @GetMapping()
    public String news()
    {
        return prefix + "/news";
    }

    /**
     * 查询新闻中心列表
     */
    @RequiresPermissions("system:news:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WNews wNews)
    {
        startPage();
        List<WNews> list = wNewsService.selectWNewsList(wNews);
        return getDataTable(list);
    }

    /**
     * 导出新闻中心列表
     */
    @RequiresPermissions("system:news:export")
    @Log(title = "新闻中心", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WNews wNews)
    {
        List<WNews> list = wNewsService.selectWNewsList(wNews);
        ExcelUtil<WNews> util = new ExcelUtil<WNews>(WNews.class);
        return util.exportExcel(list, "新闻中心数据");
    }

    /**
     * 新增新闻中心
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存新闻中心
     */
    @RequiresPermissions("system:news:add")
    @Log(title = "新闻中心", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WNews wNews)
    {
        Long maxId = wNewsService.getMaxId();
        wNews.setPrewId(maxId);
        wNewsService.insertWNews(wNews);
        // 更新上一条新闻信息
        WNews prewNews = wNewsService.selectWNewsById(maxId);
        prewNews.setNextId(wNews.getId());
        wNewsService.updateWNews(prewNews);
        return toAjax(1);
    }

    /**
     * 修改新闻中心
     */
    @RequiresPermissions("system:news:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WNews wNews = wNewsService.selectWNewsById(id);
        mmap.put("wNews", wNews);
        return prefix + "/edit";
    }

    /**
     * 修改保存新闻中心
     */
    @RequiresPermissions("system:news:edit")
    @Log(title = "新闻中心", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WNews wNews)
    {
        return toAjax(wNewsService.updateWNews(wNews));
    }

    /**
     * 删除新闻中心
     */
    @RequiresPermissions("system:news:remove")
    @Log(title = "新闻中心", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wNewsService.deleteWNewsByIds(ids));
    }
}
